package com.gymjournal

import org.junit.Test

import static junit.framework.Assert.assertNotNull
import static junit.framework.Assert.assertTrue
import static junit.framework.Assert.assertFalse
import static junit.framework.Assert.assertEquals

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
class NoteTests {

    @Test
    void testSave() {
        def note1 = new Note()
        def note2 = new Note(content: "some content")

        assertNotNull note1.save()
        assertNotNull note1.id
        assertNotNull note2.save()
        assertNotNull note2.id

        def getNote1 = Note.get(note1.id)
        assertTrue getNote1.id == note1.id
        def getNote2 = Note.findById(note2.id)
        assertTrue getNote2.content == note2.content
    }

    @Test
    void testErrorConstraint() {
        StringBuilder builder = new StringBuilder(Note.CONTENT_MAX_LENGTH + 1)
        for (int i = 0; i<Note.CONTENT_MAX_LENGTH + 1; i++) {
            builder.append('a');
        }

        def note = new Note(content: builder.toString())

        assertFalse note.validate()
        assertTrue note.hasErrors()
        assertEquals "maxSize.exceeded", note.errors.getFieldError("content").code
    }
}
